using System;
using System.Collections.Generic;
using System.Text;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.IO;

namespace My.IdlConverter.SmartHitHelper
{
    public delegate void DemandScriptFileDelegate(ref string path);
    public delegate void DemandExeFileDelegate(ref string path);

    public class CommandProcessor
    {
        public DemandScriptFileDelegate DemandScriptFileHandler;
        public DemandExeFileDelegate DemandExeFileHandler;

        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private RefereceView _referenceView;
        private Window2 _window;

        public AddIn AddIn
        {
            set { _addInInstance = value; }
            get { return _addInInstance; }
        }

        public DTE2 ApplicationObject
        {
            set { _applicationObject = value; }
            get { return _applicationObject; }
        }

        public RefereceView View
        {
            set { _referenceView = value; }
            get { return _referenceView; }
        }

        public Window2 Window
        {
            set { _window = value; }
            get { return _window; }
        }

        public void UpateUI(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
        {
            if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                if ( commandName == "My.IdlConverter.SmartHitHelper.Connect.SmartHitHelper")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
            }
        }

        public void DoCommand(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "My.IdlConverter.SmartHitHelper.Connect.SmartHitHelper")
                {
                    DoCommand();
                    handled = true;
                    return;
                }
            }
        }

        public void ExecScript()
        {
            Document activeDocument = ApplicationObject.ActiveDocument;
            if (activeDocument == null)
                return;
            string path = activeDocument.Path;
            if (path == null || !File.Exists(path))
            {
                if (DemandScriptFileHandler != null)
                {
                    DemandScriptFileHandler(ref path);
                    if (!File.Exists(path))
                        return;
                }
            }
            string exeFile = "";
            if (DemandExeFileHandler != null)
                DemandExeFileHandler(ref exeFile);
            if (File.Exists(exeFile))
            {
                try 
                { 
                    System.Diagnostics.Process.Start(exeFile); 
                }
                catch(System.Exception) {}
            }
        }

        private void DoCommand()
        {
            Window.Visible = true;
            Document document = this._applicationObject.ActiveDocument;
            if (document == null)
                return;
            TextSelection selection = (TextSelection)document.Selection;
            if ((selection == null) || (selection.IsEmpty))
                return;
        }
    }
}
